<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Logic\BookingLogic;
use PHPUnit\Exception;

class BookingController extends Controller
{
    /**
     * 拼本列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $request->validate([
            'page'      => 'sometimes|integer|min:1',
            'page_size' => 'sometimes|integer|min:1|max:100',
        ]);
        $params = $request->only(['search', 'date', 'is_low_price', 'page', 'page_size', 'user_id']);
        $logic  = new BookingLogic();
        $data   = $logic->list($params);
        return $this->OkJson($data);

    }

    /**
     * 新增拼本
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function create(Request $request): JsonResponse
    {

        $request->validate([
            'user_id'        => 'required|string',
            'user_name'      => 'required|string',
            'wechat_no'      => 'string',
            'booking_time'   => 'required|date',
            'store_name'     => 'required|string',
            'script_name'    => 'required|string',
            'script_details' => 'required|string',
            'is_api'         => 'required|in:0,1',
        ]);
        $params = $request->only(['user_id', 'user_name', 'booking_time', 'store_name', 'script_name', 'script_details', 'is_api', 'wechat_no']);
        $logic  = new BookingLogic();
        $data   = $logic->create($params);
        return $this->OkJson($data);
    }

    /**
     * 我发布的列表
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function myList(Request $request): JsonResponse
    {
        $request->validate([
            'user_id'   => 'required|string',
            'page'      => 'required|integer|min:1',
            'page_size' => 'required|integer|min:1|max:100',
        ]);
        $params = $request->only(['user_id', 'page', 'page_size']);
        $logic  = new BookingLogic();
        $data   = $logic->myList($params);
        return $this->OkJson($data);
    }

    /**
     * 修改拼本
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function update(Request $request): JsonResponse
    {
        try {
            $request->validate([
                'id'             => 'required|integer',
                'user_id'        => 'required|string',
                'user_name'      => 'required|string',
                'booking_time'   => 'required|date',
                'store_name'     => 'required|string',
                'script_name'    => 'required|string',
                'script_details' => 'required|string',
            ]);
            $params = $request->only(['id', 'user_id', 'user_name', 'booking_time', 'store_name', 'script_name', 'script_details']);
            $logic  = new BookingLogic();
            $exists = $logic->checkExists($params['id'], $params['user_id']);
            if (!$exists) {
                return $this->ErrJson('信息不存在');
            }
            $data = $logic->update($params);
            return $this->OkJson($data);
        } catch (ApiException $e) {
            return $this->ErrJson($e->getMessage());
        }

    }
}
